package com.sesame.core;

import com.sesame.util.Theme;
import freemarker.template.Configuration;
import freemarker.template.Template;
import kim.sesame.framework.web.util.FileUtil;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.util.HashMap;

public class Generate {

	/**
	 * 表生产实体
	 */
	public static void entity(HashMap<String, Object> params, String fk_type, String path) {
		String entityPackage = params.get("entityPackage").toString();
		String className = params.get("className").toString();
		path = path + "/" + entityPackage.replaceAll("\\.", "/");

//		String annotation = params.get("annotation").toString();
		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/java/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			Template temp = cfg.getTemplate("entity.flt");
			String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className + ".java", content);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成 Controller
	 */
	public static void controller(HashMap<String, Object> params, String fk_type, String path) {
		String controllerPackage = params.get("controllerPackage").toString();
		String className = params.get("className").toString();
		path = path + "/" + controllerPackage.replaceAll("\\.", "/");

		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/java/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			Template temp = cfg.getTemplate("controller.flt");
			String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className + "Controller.java", content);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成 Service
	 */
	public static void service(HashMap<String, Object> params, String fk_type, String path) {
		String servicePackage = params.get("servicePackage").toString();
		String className = params.get("className").toString();

//		String serviceImpl = params.get("serviceImpl").toString();
		String serviceImpl = "n";

		path = path + "/" + servicePackage.replaceAll("\\.", "/");

		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/java/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			if (serviceImpl.equals("y")) {
				Template temp = cfg.getTemplate("service_interface.flt");
				String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
				FileUtil.createFile(path, className + "Service.java", content);

				temp = cfg.getTemplate("service_impl.flt");
				content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
				FileUtil.createFile(path + "/" + "impl", className + "ServiceImpl.java", content);
			} else {
				Template temp = cfg.getTemplate("service.flt");
				String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
				FileUtil.createFile(path, className + "Service.java", content);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 生成 Dao,Mapper.xml
	 */
	public static void dao(HashMap<String, Object> params, String fk_type, String path) {
		String daoPackage = params.get("daoPackage").toString();
		String className = params.get("className").toString();
		path = path + "/" + daoPackage.replaceAll("\\.", "/");

		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/java/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			Template temp = cfg.getTemplate("dao.flt");
			String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className + "Dao.java", content);

			temp = cfg.getTemplate("mapper.flt");
			content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className + "DaoMapping.xml", content);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成 page
	 */
	public static void page(HashMap<String, Object> params, String fk_type, String path, String fileName,
			String pageType) {
		String packageName = params.get("packageName").toString();
		String className = params.get("className").toString();
//		path = path + "/" + packageName.replaceAll("\\.", "/");
		path = path + "/" + params.get("dirPath");
		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/page/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			Template temp = cfg.getTemplate(fileName + ".flt");
			String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className.toLowerCase() + "_" + fileName + "." + pageType, content);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成脚本
	 */
	public static void script(HashMap<String, Object> params, String fk_type, String path, String fileName) {
		String packageName = params.get("packageName").toString();
		String className = params.get("className").toString();
		//path = path + "/" + packageName.replaceAll("\\.", "/");
		path = path + "/" + params.get("dirPath");

		try {
			Configuration cfg = new Configuration();
			// 设置FreeMarker的模版文件夹位置
			String fk_path = Theme.resourcePath + "/" + fk_type + "/js/";
			cfg.setDirectoryForTemplateLoading(new File(fk_path));
			cfg.setDefaultEncoding("UTF-8");

			Template temp = cfg.getTemplate(fileName + ".flt");
			String content = FreeMarkerTemplateUtils.processTemplateIntoString(temp, params);
			FileUtil.createFile(path, className.toLowerCase() + "_" + fileName + ".js" , content);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
